package com.octlan.listviewlea;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnItemClickListener {
	private ListView	listView;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		listView = new ListView(this);

		listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, getData()));
		setContentView(listView);
		listView.setOnItemClickListener(this);
		listView.setOnScrollListener(listScrollListener);
		listView.setOnTouchListener(touchListener);
	}

	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		this.setTitle("You Click Item:" + String.valueOf(arg2));
	}

	private OnTouchListener		touchListener		= new OnTouchListener() {
														public boolean onTouch(View v, MotionEvent event) {
															int action = event.getAction();
															int x = (int) event.getRawX();
															int y = (int) event.getRawY();
															Toast.makeText(MainActivity.this, "x=" + x + ", y=" + y,
																	1000);
															return false;
														}
													};
	private OnScrollListener	listScrollListener	= new ListView.OnScrollListener() {
														@Override
														public void onScroll(AbsListView arg0, int firstVisibleItem,
																int visibleItemCount, int totalItemCount) {
															if (totalItemCount <= 0) {
																return;
															}

															if (firstVisibleItem + visibleItemCount >= totalItemCount) {
																// 当前可视第1条index + 当前可视界面存在？条 >= 总数Index 时，说明在底端 "这里到底了"
																Toast.makeText(MainActivity.this, "I am here", 1000)
																		.show();
																Log.d(MainActivity.class.getSimpleName(),
																		"to the list end");
															}
														}

														@Override
														public void onScrollStateChanged(AbsListView view,
																int scrollState) {
														}
													};

	private List<String> getData() {
		List<String> data = new ArrayList<String>();
		for (int i = 1; i <= 20; i++) {
			data.add("测试数据" + i);
		}
		return data;
	}
}
